<?php 

namespace IMT\ServicesBundle\Controller;

use JMS\Serializer\SerializationContext;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use FOS\RestBundle\View\View;
use FOS\Rest\Util\Codes;

class UserRestController extends Controller
{
	/**
	 * GET /account/{eventId}
	 *
	 * @param string $eventId
	 * @return Response
	 */
	public function getUserAction($id)
	{			
		if(intval($id) == 0)
	    	throw new \InvalidArgumentException(400, 'L\'id est incorrect : '.$id);
		
		$securityContext = $this->get('security.context');
		$user = $securityContext->getToken()->getUser();
				
		$context = new SerializationContext();
		$context->setVersion('1.0');
		$context->setGroups(array('user_details', 'user_account_list', 'user_event_list', 'user_profil_list'));

		$view = View::create()
			->setStatusCode(200)
			->setData($user)
			->setSerializationContext($context);
		
		return $this->get('fos_rest.view_handler')->handle($view);
	}
	
	public function getUserParticipantsAction($id)
	{
		if(intval($id) == 0)
			throw new \InvalidArgumentException(400, 'L\'id est incorrect : '.$id);
	
		$securityContext = $this->get('security.context');
		$user = $securityContext->getToken()->getUser();
				
		if($user->getId() != $id){
			throw new AccessDeniedException(403, 'Acces refuse pour '.$user->getUsername());
		}
	
		$arrayEvents = array();
		// recuperation des participations
    	$accounts = $this->getDoctrine()->getRepository('IMTAccountBundle:Account')->findByActiveAndUser(true, $user);
    		
    	foreach($accounts as $account){
    		foreach($account->getParticipantsEvent() as $participation){
    			$event = $participation->getEvent();
    			$owners = $event->getOwners();
    			
    			if($user->getId() != $owners[0]->getId()){
// 	    			switch ($event->getType()){
// 	    				case 'event':
	    					
// 	    					break; 
// 	    				case 'call':
// 	    					break; 
// 	    				case 'meeting':
// 	    					break; 
// 	    				case 'task':
// 	    					break; 
// 	    			}
	    			
	    			$arrayEvents[] = array('idEvent' => $event->getId(), 'idAccount' => $participation->getAccount()->getId());
    			}
    		}
    	}
    	
		$view = View::create()
		->setStatusCode(200)
		->setData($arrayEvents);
	
		return $this->get('fos_rest.view_handler')->handle($view);
	}
	
	/**
	 * Creates a new Thread from the submitted data.
	 *
	 * @param Request $request The current request
	 *
	 * @return View
	 */
	public function postUserAction(Request $request)
	{
		$em = $this->getDoctrine()->getEntityManager();
		$viewHandler = $this->get('fos_rest.view_handler');
		$um = $this->get('fos_user.user_manager');
				
		$securityContext = $this->get('security.context');
		$user = $securityContext->getToken()->getUser();
		
		$user->setGcmId($request->request->get('gcm_id'));
		
		if($request->request->get('name') || $request->request->get('firstname')){
			$profils = $user->getProfils();
			foreach($profils as $profil){
				if($request->request->get('name'))
					$profil->setName($request->request->get('name'));
				if($request->request->get('firstname'))
					$profil->setFirstname($request->request->get('firstname'));
				
				//$user->addProfil($profil);
			}
			
		}
			
		$um->updateUser($user);	
		
		return $viewHandler->handle($this->createSuccessViewResponse(array('idUser' => $user->getId())));
	}
	
	public function deleteUserAction($id)
	{
		$em = $this->getDoctrine()->getEntityManager();
		$viewHandler = $this->get('fos_rest.view_handler');			
		$um = $this->get('fos_user.user_manager');
		
// 		$securityContext = $this->get('security.context');
// 		$user = $securityContext->getToken()->getUser();
		
// 		$em->remove($entity);
// 		$em->flush();
		
		return $viewHandler->handle($this->createSuccessViewResponse(array('idUser' => 'nique ta mère !!')));
	}
	
	private function createSuccessViewResponse($return)
	{
		$view = View::create()
		->setStatusCode(200)
		->setData($return);
	
		return $view;
	}
	
	private function createErrorViewResponse($return)
	{
		$view = View::create()
		->setStatusCode(Codes::HTTP_BAD_REQUEST)
		->setData($return);
	
		return $view;
	}
}